package com.njust.service.Impl;

import com.njust.dataobject.SystemCali;
import com.njust.dataobject.SystemCaliExample;
import com.njust.dataobject.TrainParam;
import com.njust.dataobject.TrainParamExample;
import com.njust.form.CaliForm;
import com.njust.form.ParamForm;
import com.njust.mapper.SystemCaliMapper;
import com.njust.mapper.TrainParamMapper;
import com.njust.service.SysParamService;
import com.njust.vo.CaliVO;
import com.njust.vo.ParamVO;
import com.njust.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统参数表
 * @author zjwpro@foxmail.com
 * @date 2018/1/10 下午5:43
 */
@Service
@Slf4j
public class SysParamServiceImpl implements SysParamService {

    @Autowired
    private TrainParamMapper trainParamMapper;

    @Autowired
    private SystemCaliMapper systemCaliMapper;

//阈值表的增删改查
    @Transactional
    @Override
    public ResultVO paramSave(List<ParamForm> paramFormList){
        //如果数据库中没有数据那么直接插入两条 但是如果有 更新
        if (trainParamMapper.items()==0){
            //插入数据
            for (ParamForm paramForm:paramFormList){
                TrainParam trainParam=new TrainParam();
                BeanUtils.copyProperties(paramForm,trainParam);
                trainParamMapper.insert(trainParam);
            }
            return ResultVO.createBySuccess("保存成功");
        }
        else {
            for (ParamForm paramForm:paramFormList){
                TrainParam trainParam=new TrainParam();
                BeanUtils.copyProperties(paramForm,trainParam);
                if(trainParam.getUdFlag().equals(0)){
                    //上行
                    TrainParamExample example=new TrainParamExample();
                    TrainParamExample.Criteria criteria = example.createCriteria();
                    criteria.andUdFlagEqualTo(0);
                    trainParamMapper.updateByExampleSelective(trainParam, example);
                }else{
                    //下行
                    TrainParamExample example=new TrainParamExample();
                    TrainParamExample.Criteria criteria = example.createCriteria();
                    criteria.andUdFlagEqualTo(1);
                    trainParamMapper.updateByExampleSelective(trainParam, example);
                }
            }
            return ResultVO.createBySuccess("更新成功");
        }
    }

    /**
     * 无数据
     * @return
     */
    //先要选出上行 和下行的最后一条记录 作为回显
    @Override
    public ResultVO<List<ParamVO>> showParam(){

        //返回的list数组
        List<ParamVO> paramVOList=new ArrayList<>();

        //返回的数据
        ParamVO upparamVO=new ParamVO();
        ParamVO downparamVO=new ParamVO();
        //上行数据
        TrainParam uptrainParam = trainParamMapper.findByUdFlag(0);
        if(uptrainParam!=null){
            BeanUtils.copyProperties(uptrainParam,upparamVO);
            paramVOList.add(upparamVO);
        }

        //下行数据
        TrainParam downtrainParam = trainParamMapper.findByUdFlag(1);
        if(downtrainParam!=null){
            BeanUtils.copyProperties(downtrainParam,downparamVO);
            paramVOList.add(downparamVO);
        }
        return ResultVO.createBySuccess(paramVOList);
    }
//标定数据表的增删改查
    @Override
    public ResultVO caliSave(List<CaliForm> caliFormList) {
        //如果数据库中没有数据那么直接插入两条 但是如果有 更新
        if (systemCaliMapper.items()==0){
            //插入数据
            for (CaliForm caliForm:caliFormList){
                SystemCali systemCali=new SystemCali();
                BeanUtils.copyProperties(caliForm,systemCali);
                systemCaliMapper.insert(systemCali);
            }
            return ResultVO.createBySuccess("保存成功");
        }
        else {
            for (CaliForm caliForm:caliFormList){
                SystemCali systemCali=new SystemCali();
                BeanUtils.copyProperties(caliForm,systemCali);
                if(systemCali.getUdFlag().equals(0)){
                    //上行
                    SystemCaliExample example=new SystemCaliExample();
                    SystemCaliExample.Criteria criteria = example.createCriteria();
                    criteria.andUdFlagEqualTo(0);
                    systemCaliMapper.updateByExampleSelective(systemCali, example);
                }else{
                    //下行
                    SystemCaliExample example=new SystemCaliExample();
                    SystemCaliExample.Criteria criteria = example.createCriteria();
                    criteria.andUdFlagEqualTo(1);
                    systemCaliMapper.updateByExampleSelective(systemCali, example);
                }
            }
            return ResultVO.createBySuccess("更新成功");
        }
    }

    @Override
    public ResultVO showCail() {

        //返回封装的对象
        List<CaliVO> caliVOList=new ArrayList<>();

        CaliVO upCaliVO=new CaliVO();
        CaliVO downCaliVO=new CaliVO();

        SystemCali upsys = systemCaliMapper.findByUdFlag(0);
        SystemCali downsys = systemCaliMapper.findByUdFlag(1);
        if(upsys!=null){
            BeanUtils.copyProperties(upsys,upCaliVO);
            caliVOList.add(upCaliVO);
        }
        if(upsys!=null){
            BeanUtils.copyProperties(downsys,downCaliVO);
            caliVOList.add(downCaliVO);
        }
        return ResultVO.createBySuccess(caliVOList);
    }
}
